package cn.lixu.system.service.impl;

import cn.lixu.basic.service.impl.BaseServiceImpl;
import cn.lixu.system.domain.Role;
import cn.lixu.system.mapper.RoleMapper;
import cn.lixu.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author LiXu
 * @date 2022/08/02 0002 19:52:13
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {


    @Autowired
    private RoleMapper roleMapper;

   /* @Override
    public List<Role> roleTree() {
        //获取全部部门信息
        List<Role> list = roleMapper.loadAll();
        //准备一个map集合，用来装所有的部门
        HashMap<Long, Role> map = new HashMap<>();
        //遍历所有的部门信息
        for (Role role : list) {
            //将部门信息装入map集合中
            map.put(role.getId(), role);
        }

        List<Role> roleTree = new ArrayList<>();
        for (Role role : list) {
            if (role.getId() == null) {//顶级部门
                roleTree.add(role);
            } else {
                //获取不是顶级部门的id
                Long parent_id = role.getId();
                //通过id在map里面去获取响应的部门信息
                Role parentRole = map.get(parent_id);
                //将自己添加到上级部门里面
                parentRole.getChildren().add(role);
            }
        }
        return roleTree;
    }*/


    /**
     * 重写保存方法
     *
     * @param role
     */
    @Override
    public void save(Role role) {
        roleMapper.save(role);
        //添加到RolePermission中
        if (role.getPermissions().size() > 0) {
            roleMapper.saveRolePermission(role.getId(), role.getPermissions());
        }
        //添加到RolePermission中
        if (role.getMenus().size() > 0) {
            roleMapper.saveRolemenu(role.getId(), role.getMenus());
        }
    }

    /**
     * 重写删除的方法
     *
     * @param rid
     */
    @Override
    public void remove(Long rid) {

        //通过role的id删除EmployeeRole
        roleMapper.removeEmployeeRole(rid);
        //通过role的id删除RoleMenu
        roleMapper.removeRoleMenu(rid);
        //通过role的id删除RolePermission
        roleMapper.removeRolePermission(rid);

        // 删除自己
        roleMapper.remove(rid);

    }
}
